package com.summer.safe.quartz.service;

import com.summer.safe.quartz.dao.QuartzJobDao;
import com.summer.safe.quartz.utils.QuartzUtils;
import com.summer.safe.base.BaseService;
import com.summer.safe.config.result.ResultCode;
import com.summer.safe.exception.CustomException;
import com.summer.safe.quartz.entity.QuartzJob;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;


/**
 * 定时任务调度 Service
 *
 * @author lm
 * @date 2020-07-23 14:27:23
 */
@Service
public class QuartzJobService extends BaseService<QuartzJobDao, QuartzJob> {
    //加入Qulifier注解，通过名称注入bean
    @Resource(name = "scheduler")
    private Scheduler scheduler;
    /**
     * 初始化，动态定时器
     *
     * @throws Exception
     */
    @PostConstruct
    public void init() throws Exception {
        QuartzJob quartzJob =new QuartzJob();
        quartzJob.setStatus(quartzJob.STATUS_RUNNING);
        // 这里获取任务信息数据
        List<QuartzJob> jobList = dao.findAllList(quartzJob);
        for (QuartzJob job : jobList) {
            QuartzUtils.addJob(job,scheduler);
        }
    }
    /**
     * 新增一个任务
     */
    public void saveAJobNow(QuartzJob quartzJob) throws SchedulerException {
        QuartzUtils.addJob(quartzJob,scheduler);

    }

    @Override
    public void save(QuartzJob quartzJob)  {
        if (quartzJob.getIsNewRecord()) {
            quartzJob.preInsert();
            try {
                dao.insert(quartzJob);
            }catch ( Exception e){
                logger.error("抛出的异常{}", e.getMessage());
                throw new CustomException(ResultCode.FAIL_SAVE);
            }

        } else {
            quartzJob.preUpdate();
            try {
                //更新状态
                QuartzUtils.updateJobCron(quartzJob,scheduler);
                dao.update(quartzJob);
            }catch ( Exception e){
                logger.error("抛出的异常{}", e.getMessage());
                throw new CustomException(ResultCode.FAIL_SAVE);
            }
        }
        try {
            this.saveAJobNow(quartzJob);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }
    /**
     * 立即执行job
     *
     * @throws SchedulerException
     */
    public void runAJobNow(QuartzJob quartzJob) throws SchedulerException {
        QuartzUtils.runAJobNow(quartzJob,scheduler);
        //更新任务状态
        this.updateJobStatus(quartzJob);
    }

    /**
     * 暂停一个job
     *
     * @param quartzJob
     * @throws SchedulerException
     */
    public void pauseJob(QuartzJob quartzJob) throws SchedulerException {

        QuartzUtils.pauseJob(quartzJob,scheduler);
        //更新任务状态
        this.updateJobStatus(quartzJob);
    }

    /**
     * 恢复一个job
     *
     * @param quartzJob
     * @throws SchedulerException
     */
    public void resumeJob(QuartzJob quartzJob) throws SchedulerException {

        QuartzUtils.resumeJob(quartzJob,scheduler);
        //更新任务状态
        this.updateJobStatus(quartzJob);

    }


    /**
     * 删除一个job
     *
     * @param quartzJob
     * @throws SchedulerException
     */
    public void deleteJob(QuartzJob quartzJob)  {
        //删除
        dao.delete(quartzJob);
        //更新任务状态
        this.updateJobStatus(quartzJob);

    }

    /**
     * 更改任务状态
     *
     * @param quartzJob
     * @throws SchedulerException
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public  void updateJobStatus(QuartzJob quartzJob)  {
        dao.updateJobStatus(quartzJob);
        quartzJob=dao.get(quartzJob);
        QuartzUtils.updateJobStatus(quartzJob,scheduler);

    }



}  